package com.smilive.start.netty.chat.server.handler;

import com.smilive.start.netty.chat.message.LoginRequestMessage;
import com.smilive.start.netty.chat.message.LoginResponseMessage;
import com.smilive.start.netty.chat.server.UserService;
import com.smilive.start.netty.chat.server.impl.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import sun.reflect.misc.ReflectUtil;

/**
 * @program: netty
 * @Date: 2022-01-06 14:51
 * @Author: smilive
 * @Description: 登入业务处理
 */
public class LoginRequestMessageHandler extends SimpleChannelInboundHandler<LoginRequestMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginRequestMessage msg) throws Exception {
        String name = msg.getName();
        String pwad = msg.getPwad();
        UserService userService = (UserService) ReflectUtil.newInstance(UserServiceImpl.class);
        boolean login = userService.login(name, pwad);
        LoginResponseMessage message;
        if (login) {
            message = new LoginResponseMessage(true, "登陆成功");
        } else {
            message = new LoginResponseMessage(false, "登陆失败");
        }
        ctx.writeAndFlush(message);
    }
}
